package org.lds.ers.server;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.lds.ers.client.WardService;
import org.lds.ers.client.model.WardERSCInfo;
import org.lds.ers.client.model.WardInfo;
import org.lds.ers.server.model.PMF;
import org.lds.ers.server.model.Stake;
import org.lds.ers.server.model.StakeERS;
import org.lds.ers.server.model.Ward;
import org.lds.ers.server.model.WardERS;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class WardServiceImpl extends RemoteServiceServlet implements WardService {

	public Boolean delete(Long id) {
		Boolean result = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			Ward w = pm.getObjectById(Ward.class, id);

			pm.deletePersistent(w);
			result = true;
		}
		catch (Exception ex) {
			result = false;
		}
		finally {
			pm.close();
		}
		return result ;
	}

	public Boolean deleteERSC(Long id) {
		Boolean result = false;
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			WardERS s = pm.getObjectById(WardERS.class, id);

			pm.deletePersistent(s);
			result = true;
		}
		catch (Exception ex) {
			result = false;
		}
		finally {
			pm.close();
		}
		return result ;
	}

	public List<WardInfo> getAll(Long stakeID) {
		List<WardInfo> wards = new ArrayList<WardInfo>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery(Ward.class, "stakeID == theStakeID ");
		query.declareParameters("Long theStakeID");

	    List<Ward> ss = (List<Ward>) query.execute(stakeID);
 	    for (Ward s : ss) {
	    	WardInfo si = new WardInfo();
	    	if (s.getStake() != null)
	    		si.setWardName(s.getStake().getStakeName());
	    	
	    	si.setID(s.getId());
	    	si.setWardName(s.getWardName());
	    	si.setWardNo(s.getWardNo());
	    	si.setRemark(s.getRemark());
	    	si.setLastUpdate(s.getLastUpdate());
	    	si.setByWhom(s.getByWhom());
	    	wards.add(si);
	    }
	    
		return wards;
	}

	public List<WardERSCInfo> getAllERSC(Long wardID) {
		List<WardERSCInfo> erscs = new ArrayList<WardERSCInfo>();
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    //String query = "select from " + StakeERS.class.getName() + " order by userID asc";
		Query query = pm.newQuery(WardERS.class, "wardID == theWardID ");
		query.declareParameters("Long theWardID");

	    List<WardERS> ss = (List<WardERS>) query.execute(wardID);
	    for (WardERS s : ss) {
	    	WardERSCInfo si = new WardERSCInfo();
	    	si.setID(s.getID());
	    	si.setWardID(s.getWardID());
	    	si.setUserID(s.getUserID());
	    	si.setRealName(s.getRealName());
	    	
	    	erscs.add(si);
	    }
	    
		return erscs;
	}

	public WardInfo insert(WardInfo ward) {
		WardInfo result = null;
		Ward s = new Ward(ward.getWardName(), ward.getWardNo(), ward.getStakeID(), ward.getRemark(), new Date(), ward.getByWhom());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		Stake stake = pm.getObjectById(Stake.class, s.getStakeID());
		s.setStake(stake);
		try
		{
			pm.makePersistent(s);
			ward.setID(s.getId());
			result = ward;
		}
		catch (Exception ex) {
			result = null;
		}
		finally {
			pm.close();
		}
		
		return result ;
	}

	public WardERSCInfo insertERSC(WardERSCInfo ersc) {

		WardERSCInfo result = null;
		WardERS s = new WardERS(ersc.getWardID(), ersc.getUserID(), ersc.getRealName());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			pm.makePersistent(s);
			ersc.setID(s.getID());
			result = ersc;
		}
		catch (Exception ex) {
			result = null;
		}
		finally {
			pm.close();
		}
		
		return result ;
	}

	public Boolean update(WardInfo ward) {
		Boolean result = false;
		Ward s = new Ward(ward.getWardName(), ward.getWardNo(), ward.getStakeID(),  ward.getRemark(), new Date(), ward.getByWhom());
		s.setID(ward.getId());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			pm.makePersistent(s);
			result = true;
		}
		catch (Exception ex) {
			result = false;
		}
		finally {
			pm.close();
		}
		return result ;
	}

	public Boolean updateERSC(WardERSCInfo ersc) {
		Boolean result = false;
		WardERS s = new WardERS(ersc.getWardID(), ersc.getUserID(), ersc.getRealName());
		s.setID(ersc.getID());
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			pm.makePersistent(s);
			result = true;
		}
		catch (Exception ex) {
			result = false;
		}
		finally {
			pm.close();
		}
		return result ;
	}

	public List<WardInfo> getMyWards(String userID) {
		List<WardInfo> wards = new ArrayList<WardInfo>();
		
		
		return wards;
	}


	
	
}
